layui.use(['table', 'admin', 'ax','simTool'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var simtool = layui.simTool;

    /**
     * 农场视频表管理
     */
    var FactoryVideo = {
        tableId: "factoryVideoTable"
    };

    var sim = new simtool("","");
    sim.updateButton("/factoryVideo/editItem",FactoryVideo.tableId,"status",[1,0])
    /**
     * 初始化表格的列
     */
    FactoryVideo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'factoryName', sort: true, title: '农场名'},
            {field: 'title', sort: true, title: '标题'},
            {field: 'description', sort: true, title: '图文详情'},
            {field: 'url', sort: true, title: '链接'},
            {field: 'image', sort: true,hide: true, title: '图片'},
            {field: 'status', sort: true, title: '状态',templet:"#statusTpl"},
            {field: 'weigh', sort: true, title: '排序'},
            {field: 'createTime', sort: true, title: '新增时间'},
            {field: 'updateTime', sort: true,hide: true, title: '修改时间'},
            {field: 'recordStatus', sort: true, hide: true,title: '记录状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth:180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FactoryVideo.search = function () {
        var queryData = {};
        queryData['title'] = $("#title").val();
        queryData['factoryId'] = $("#factoryId").val();
        table.reload(FactoryVideo.tableId, {where: queryData,
            curr:{page:1}
        });
    };

    /**
     * 弹出添加对话框
     */
    FactoryVideo.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加农场视频表',
            maxmin:true,
            area:["70%","80%"],
            content: Feng.ctxPath + '/factoryVideo/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(FactoryVideo.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    FactoryVideo.exportExcel = function () {
        var checkRows = table.checkStatus(FactoryVideo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FactoryVideo.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改农场视频表',
            maxmin:true,
            area:["70%","80%"],
            content: Feng.ctxPath + '/factoryVideo/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(FactoryVideo.tableId);
            }
        });
    };

    /**
     * 配置闲时视频
     * @param data
     */

    FactoryVideo.onConfigIdleVideoItem = function (data) {
        admin.putTempData('formOk', false);
        layui.admin.open({
            type: 2,
            title: '农场非直播视频表',
            maxmin:true,
            area:["100%","100%"],
            content: Feng.ctxPath + '/factoryVideoTime/?factoryVideoId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(FactoryVideo.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FactoryVideo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/factoryVideo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(FactoryVideo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    var urlParam = Feng.getUrlParam("factoryId");
    if(urlParam==""||urlParam== null){
        urlParam=0;
    }
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FactoryVideo.tableId,
        url: Feng.ctxPath + '/factoryVideo/list?factoryId='+urlParam,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: FactoryVideo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FactoryVideo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        FactoryVideo.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FactoryVideo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + FactoryVideo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FactoryVideo.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FactoryVideo.onDeleteItem(data);
        }else if (layEvent === 'idleVideo') {
            FactoryVideo.onConfigIdleVideoItem(data);
        }
    });
});
